package com.dkcrow.spring.transaction.aop.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import com.dkcrow.spring.transaction.AspectJTransactionApp;
import com.dkcrow.spring.transaction.TransactionApp;
import com.dkcrow.spring.transaction.aop.MyMethodInvocation;

@Aspect
public class TransactionAdvisor {
	
	@Pointcut("@annotation(com.dkcrow.spring.anno.MyAnnotation) && execution(* *())")
	public void imDatabase() {
	}
	
	@Pointcut("@annotation(com.dkcrow.spring.anno.MyLocalAnnotation) && execution(* *())")
	public void localDatabase() {
	}
	
	@Around("imDatabase()")
	public void imDatabaseAround(ProceedingJoinPoint pjp) throws Throwable {
		doTr(pjp);
	}
	
	@Around("localDatabase()")
	public void localDatabaseAround(ProceedingJoinPoint pjp) throws Throwable {
		doTr(pjp);
	}

	private void doTr(ProceedingJoinPoint pjp) throws Throwable {
		MyMethodInvocation myMethodInvocation = MyMethodInvocation.newInstance(pjp);
		TransactionInterceptor ti = (TransactionInterceptor) TransactionApp.ap.getBean("myAdvice");
		ti.invoke(myMethodInvocation);
	}
}